<?php
$hide = $_REQUEST["hide"];
$conn = mysqli_connect("127.0.0.1", "root", "", "aiyedb");
mysqli_query($conn, "SET NAMES utf8");
if ($hide == "register") {     //注册
	$name = $_POST["name"];
	$pwd = $_POST["pwd"];
	$phone = $_POST["phone"];
	$sql = "SELECT user_name FROM userinfo";
	$rs = mysqli_query($conn, $sql);
	while (($row = mysqli_fetch_assoc($rs)) != null) {
		if ($row["user_name"] == $name) {
			echo "<script>sessionStorage.state='注册失败';sessionStorage.html='regsiter.html';location='../status.html'</script>";
			return false;
		}
	}
	$sql = "INSERT INTO userinfo VALUES(0,'$name','$pwd','$phone','','0')";
	$rs = mysqli_query($conn, $sql);
	if ($rs) {
		echo "<script>sessionStorage.state='注册成功';sessionStorage.html='login.html';location='../status.html'</script>";
	}
} else if ($hide == "login") {   //登录
	$name = $_POST["name"];
	$pwd = $_POST["pwd"];
	$sql = "SELECT * FROM userinfo";
	$rs = mysqli_query($conn, $sql);
	while (($row = mysqli_fetch_assoc($rs)) != null) {
		if ($row["user_name"] == $name && $row["user_pwd"] == $pwd) {
			$arr = [];
			array_push($arr, $row);
			$json = json_encode($arr);
			if($row['jurisdiction']=="0"){
				echo "<script>sessionStorage.setItem('jsonKey',JSON.stringify($json));sessionStorage.jurisdiction='0';sessionStorage.name='$name';sessionStorage.state='登录成功';sessionStorage.html='index.html';location='../status.html'</script>";
			}else{
				echo "<script>sessionStorage.setItem('jsonKey',JSON.stringify($json));sessionStorage.jurisdiction='1';sessionStorage.name='$name';location='../management/index.html'</script>";
			}
			
			return false;
		}
	}
	echo "<script>sessionStorage.state='登录失败';sessionStorage.html='login.html';location='../status.html'</script>";
} else if ($hide == "myinfo") {    //查看个人信息
	$id = $_get["id"];
	$sql = "SELECT * FROM userinfo where id=$id";
	$rs = mysqli_query($conn, $sql);
	while (($row = mysqli_fetch_row($rs)) != null) {
		echo JSON_encode($row);
	}
} else if ($hide == "getnames") {
	$sql = "SELECT user_name FROM userinfo";
	$rs = mysqli_query($conn, $sql);
	$arr = [];
	while (($row = mysqli_fetch_assoc($rs)) != null) {
		array_push($arr, $row);
	}
	$json = json_encode($arr);
	echo $_GET["key"]."(".$json.")";
}else if ($hide == "registerht") {     //注册
	$name = $_POST["name"];
	$pwd = $_POST["pwd"];
	$phone = $_POST["phone"];
	$sql = "SELECT user_name FROM userinfo";
	$rs = mysqli_query($conn, $sql);
	while (($row = mysqli_fetch_assoc($rs)) != null) {
		if ($row["user_name"] == $name) {
			echo "注册失败";
			return false;
		}
	}
	$sql = "INSERT INTO userinfo VALUES(0,'$name','$pwd','$phone','','0')";
	$rs = mysqli_query($conn, $sql);
	if ($rs) {
		echo "注册成功";
	}
}
// else if($hide=="changemyinfo"){
// 	$id=$_get["id"];
// 	$phone=$_GET["phone"];
// 	$pwd = $_POST["pwd"];
// 	$npwd = $_POST["npwd"];
// 	$sql = "SELECT * FROM userinfo";
// 	$rs = mysqli_query($conn, $sql);
// 	while (($row = mysqli_fetch_assoc($rs)) != null) {
// 		if ($row["id"] == $id && $row["pwd"] == $pwd) {
// 			$sql = "UPDATE user SET pwd='$npwd' WHERE id=$id";
// 			$rs = mysqli_query($conn, $sql);
// 			if ($rs) {
// 				$sql = "SELECT * FROM user";
// 				$rs = mysqli_query($conn, $sql);
// 				while (($row = mysqli_fetch_assoc($rs)) != null) {
// 					if ($row["id"] == $id) {
// 						$arr = [];
// 						array_push($arr, $row);
// 						$json = json_encode($arr);
// 						echo "<script>sessionStorage.state='修改成功';sessionStorage.html='personal.html';sessionStorage.setItem('jsonKey',JSON.stringify($json));location='html/status.html'</script>";
// 						return false;
// 					}
// 				}
// 			}
// 		}
// 	}
// 	echo "<script>sessionStorage.state='修改失败';sessionStorage.html='personal.html';location='html/status.html'</script>";
// }